<?php
//Knowledge Management Controller By Teerapong Singthong.
class Blog extends Controller{
	public $data 	= 	array();
	public function __construct()
	{
		parent::Controller();
		
		$this->load->helper('ckeditor');
		//Ckeditor's configuration
		$this->data['ckeditor'] = array(
		'id' 	=> 	'content',
		'path'	=>	'js/ckeditor',
		//Optionnal values
		'config' => array(
		'toolbar' 	=> 	"Full", 	//Using the Full toolbar
		'width' 	=> 	"550px",	//Setting a custom width
		'height' 	=> 	'100px',	//Setting a custom height
		),
		//Replacing styles from the "Styles tool"
		'styles' => array(
		//Creating a new style named "style 1"
		'style 1' => array (
		'name' 		=> 	'Blue Title',
		'element' 	=> 	'h2',
		'styles' => array(
		'color' 	=> 	'Blue',
		'font-weight' 	=> 	'bold'
		)
			),
		//Creating a new style named "style 2"
		'style 2' => array (
		'name' 	=> 	'Red Title',
		'element' 	=> 	'h2',
		'styles' => array(
		'color' 		=> 	'Red',
		'font-weight' 		=> 	'bold',
		'text-decoration'	=> 	'underline'
		)
			)
			)
			);
		$this->data['ckeditor_2'] = array(
		//ID of the textarea that will be replaced
		'id' 	=> 	'content_2',
		'path'	=>	'js/ckeditor',
		//Optionnal values
		'config' => array(
		'width' 	=> 	"865px",	//Setting a custom width
		'height' 	=> 	'150px',	//Setting a custom height
		'toolbar' 	=> 	array(	//Setting a custom toolbar
		array('Source','-','Save','NewPage','Preview','-','Templates'),
		array('Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'),
		array('Undo','Redo','-','Bold', 'Italic'),
		array('Underline', 'Strike'),
		array('Find','Replace','-','SelectAll','RemoveFormat'),
		array('NumberedList','BulletedList','-','Outdent','Indent','Blockquote','CreateDiv'),
		array('JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'),
		array('Link','Unlink','Anchor'),
		array('Image','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'),
		array('Styles','Format','Font','FontSize'),
		array('TextColor','BGColor'),
		array('-','Maximize'),
		'/'
		)
			),
		//Replacing styles from the "Styles tool"
		'styles' => array(
		//Creating a new style named "style 1"
		'style 3' => array (
		'name' 		=> 	'Green Title',
		'element' 	=> 	'h3',
		'styles' => array(
		'color' 	=> 	'Green',
		'font-weight' 	=> 	'bold'
		)
			)
			)
			);
			
			
		//ใช้กับบล็อก
		$this->data['ckeditor_3'] = array(
		//ID of the textarea that will be replaced
		'id' 	=> 	'content_3',
		'path'	=>	'js/ckeditor',
		//Optionnal values
		'config' => array(
		'width' 	=> 	"565px",	//Setting a custom width
		'height' 	=> 	'60px',	//Setting a custom height
		'toolbar' 	=> 	array(	//Setting a custom toolbar
		array('Save','NewPage','Preview','-'),
		array('Link','Unlink'),
		array('Smiley'),
		)
			),
		//Replacing styles from the "Styles tool"
		'styles' => array(
		//Creating a new style named "style 1"
		'style 3' => array (
		'name' 		=> 	'Green Title',
		'element' 	=> 	'h3',
		'styles' => array(
		'color' 	=> 	'Green',
		'font-weight' 	=> 	'bold'
		)
			)
			)
			);	
	}
	
	function index(){

		//models.
		$this->load->model('blog_model');	//load model.
		$data=$this->blog_model->setdata();	//load model->setdata() function.

		//views.
		$this->load->view('inc/blog/c_blog',$data);	//load view content.
	}
	
	function is_logged_in(){
		$is_logged_in = $this->session->userdata('is_logged_in');
	    if(!isset($is_logged_in) || $is_logged_in != true)
	    {
                /*$data["title"] = "Permission Deny.";
                $data["topic"] = "You don have permission to access this page.";
				$data["content"]='msg/permission_deny';
				$views = $this->load->view('inc/one_cols',$data);	//load view one cols.*/	
				$path = base_url();
				redirect($path);				
	    }
	}
	
	function category($id){
		$data['id'] = $id;
		$data['title'] = "Blog Category";
		$data["navigator"] = "&nbsp;&#8250;&nbsp;Blog Node&nbsp;&#8250;&nbsp;Blog Category";
		$this->load->view('inc/blog/view_by_category',$data);
	}
	
	function create_blog_by_category_id($id){
		$this->is_logged_in();	//Check login.
		$this->data['id'] = $id;
		$this->data['title'] = "Create New Entry";
		$this->data['navigator'] = "&nbsp;&#8250;&nbsp;Blog Node&nbsp;&#8250;&nbsp;Create New entry";
		$this->load->view('inc/blog/create_blog',$this->data);
	}
	
	function add_entry(){
		$this->is_logged_in();	//Check login.
		$this->load->model('blog_model');
		$query = $this->blog_model->add_entry();
		
		if($query){
			echo "ok";
		}
		else
		{
			echo "false";
		}
	}
	
	function read_by_blog_id($id){
		//this loaded
			$this->dd['id'] = $id;
			$this->load->model('blog_model');
			$query_log = $this->blog_model->add_views($this->dd);
			if($query_log){
				$this->data['id'] = $id;
				$this->data['title'] = "Read Blog Content.";
				$this->data['navigator'] = "&nbsp;&#8250;&nbsp;Blog Node&nbsp;&#8250;&nbsp;Blog Content Detail";
				$this->load->view('inc/blog/view_blog_by_id',$this->data);
			}
			else{
				return false;
			}
		//end
		
	}
	
	function my_blog(){
		$data['id'] =  $this->session->userdata('member_id');
		$data['title'] = "My Blog.";
		$data["navigator"] = "&nbsp;&#8250;&nbsp;Blog Node&nbsp;&#8250;&nbsp;My Blogs";
		$this->load->view('inc/blog/view_my_blog',$data);
	}
	
	function delete_my_blog_id($id){
		$this->db->where('blog_id',$id);
		$this->db->delete('blog');
		$query = $this->db->affected_rows();
		
		if($query){
			$path = base_url()."blog";
			redirect($path);
		}
		else
		{
			echo "false";
		}
	}
	
	function all_blog(){
		$data['title'] = "All Blog.";
		$data["navigator"] = "&nbsp;&#8250;&nbsp;Blog Node&nbsp;&#8250;&nbsp;All Blogs";
		$this->load->view('inc/blog/view_all_blog',$data);
	}
	
	function add_comment(){
		$this->load->model('blog_model');
		$query = $this->blog_model->add_comment();
		
		if($query){
			echo "ok";
		}
		else
		{
			echo "false";
		}
	}
	
	function vote_up(){
		//$member_id = $this->input->post('member_id');
		$blog_id = $this->input->post('blog_id');
		$this->load->model('blog_model');
		$query = $this->blog_model->vote_up();
		
		if($query){
			$this->db->select_sum('like_vote');
			$this->db->where('blog_id',$blog_id);
			$query_like_vote = $this->db->get('blog_vote');
			foreach($query_like_vote->result() as $i){
				$like_vv = $i->like_vote;
			}
			echo $like_vv;
		}
		else
		{
			return false;
		}
	}
	
	function vote_down(){
		//$member_id = $this->input->post('member_id');
		$blog_id = $this->input->post('blog_id');
		$this->load->model('blog_model');
		$query = $this->blog_model->vote_down();
		
		if($query){
			$this->db->select_sum('dislike_vote');
			$this->db->where('blog_id',$blog_id);
			$query_dislike_vote = $this->db->get('blog_vote');
			foreach($query_dislike_vote->result() as $o){
				$dislike_vv = $o->dislike_vote;
			}
			echo $dislike_vv;
		}
		else
		{
			return false;
		}
	}
	
	function delete_comment($id, $blog_id, $nickname){
		$data['id'] = $id;
		$this->load->model('blog_model');
		$query = $this->blog_model->delete_comment($data);
		
		if($query){
			$path = base_url()."blog/".$nickname."/entry/".$blog_id;
			redirect($path);
		}
		else
		{
			echo "false";
		}
	}
	
	function edit_my_blog_id($id){
		$this->is_logged_in();	//Check login.
		$this->data['id'] = $id;
		$this->data['title'] = "Create New Entry";
		$this->load->view('inc/blog/edit_blog',$this->data);
	}
	
	function update_entry(){
		$this->is_logged_in();	//Check login.
		$this->load->model('blog_model');
		$query = $this->blog_model->update_entry();
		
		if($query){
			echo "ok";
		}
		else
		{
			echo "false";
		}
	}
	
}